514. Time for Nicholas

 

Vasilyek know that St. Nicholas starts to deliver gifts when it gets dark, and finishes when it dawns. He wonders: how much time will have Nicholas to deliver all his gifts.

 

Input. In a single line given t1 and t2 time when Nicholas starts and when finishes to deliver presents in format hh : mm : ss.

 

Output. Print the time that St. Nicholas uses to deliver gifts in the format hh : mm : ss. It is guaranteed that this time does not exceed 12 hours.

 

Sample input

Sample output

19:00:00 21:00:00

02:00:00

 

 

SOLUTION

elementary

 

Algorithm analysis

Convert the start and end times to seconds. The time hh : mm : ss corresponds to 3600 * hh + 60 * mm + ss seconds.

Compute the difference t2t1 between them. If it is negative, then midnight belongs to the time when Nikolas delivers gifts. In this case, 3600 * 24 should be added to the negative difference – the number of seconds in a day. Next, compute how many hours, minutes and seconds the found difference is.

Let d be the time of gifts delivery in seconds. Convert it to hours h, minutes m and seconds s is the same as to represent the number d in sexagesimal notation:

·        h = d / 3600;

·        m = (d % 3600) / 60;

·        s = d % 60;

For d = 18886 we have:

·        h = 18886 / 3600 = 5;

·        m = (18886 % 3600) / 60 = 14;

·        s = 18886 % 60 = 46;

 

Algorithm realization

Read the input data. Convert the start and end times to seconds.

 

scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);

t1 = h1 * 3600 + m1 * 60 + s1;

t2 = h2 * 3600 + m2 * 60 + s2;

 

Compute the difference between the times t2 and t1 in seconds. If t2 < t1, then t2 belongs to the next day.

 

d = t2 - t1;

if (d < 0) d += 3600*24;

// d = (t2 - t1 + 3600*24) % (3600*24);

 

Convert the difference d to hours h3, minutes m3 and seconds s3.

 

h3 = d / 3600;

m3 = (d % 3600) / 60;

s3 = d % 60;

 

Print the answer in time format. The %02d format means to print a two digit integer. Moreover, if the number is a single digit, then 0 will be displayed before it. For example, if the number of minutes is 4, then 04 will be displayed. If the number of minutes is 0, then 00 will be displayed.

 

printf("%02d:%02d:%02d\n",h3,m3,s3);

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.next();

    StringTokenizer st1 = new StringTokenizer(s, ":");

    int h1 = Integer.parseInt(st1.nextToken());

    int m1 = Integer.parseInt(st1.nextToken());

    int s1 = Integer.parseInt(st1.nextToken());

 

    s = con.next();

    st1 = new StringTokenizer(s, ":");

    int h2 = Integer.parseInt(st1.nextToken());

    int m2 = Integer.parseInt(st1.nextToken());

    int s2 = Integer.parseInt(st1.nextToken());

 

    int t1 = h1 * 3600 + m1 * 60 + s1;

    int t2 = h2 * 3600 + m2 * 60 + s2;

   

    int d = (t2 - t1 + 3600*24) % (3600*24);

   

    int h3 = d / 3600; d = d % 3600;

    int m3 = d / 60;

    int s3 = d % 60;

    System.out.printf("%02d:%02d:%02d\n",h3,m3,s3);

    con.close();

  }

}